package kr.saeildamil.androidstudy.ch13;

import java.util.Calendar;
import java.util.GregorianCalendar;

import kr.saeildamil.androidstudy.R;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class PickerDailogTest extends Activity {
	int mYear;
	int mMonth;
	int mDay;
	int mHour;
	int mMinute;
	TextView mDateTv;
	TextView mTimeTv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch13_pickerdialog);

		mDateTv = (TextView) findViewById(R.id.ch13_pickerdailog_txtfile);
		mTimeTv = (TextView) findViewById(R.id.ch13_pickerdailog_txttime);

		Calendar cal = new GregorianCalendar();
		mYear = cal.get(Calendar.YEAR);
		mMonth = cal.get(Calendar.MONTH);
		mDay = cal.get(Calendar.DAY_OF_MONTH);
		mHour = cal.get(Calendar.HOUR);
		mMinute = cal.get(Calendar.MINUTE);

		updateNow();
	}

	private void updateNow() {
		mDateTv.setText(String.format("%d/%d/%d", mYear, mMonth, mDay));
		mTimeTv.setText(String.format("%d:%d", mHour, mMinute));
	}

	public void mOnClick(View v) {
		switch (v.getId()) {
		case R.id.ch13_pickerdialog_datebtn:
			new DatePickerDialog(this, mDateSetListener, mYear, mMonth+1, mDay)
					.show();
			break;
		case R.id.ch13_pickerdailog_timebtn:
			new TimePickerDialog(this, mTimeSetListener, mHour, mMinute, false)
					.show();
			break;
		}
	}

	TimePickerDialog.OnTimeSetListener mTimeSetListener = new OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			mHour = hourOfDay;
			mMinute = minute;
			updateNow();
		}
	};

	DatePickerDialog.OnDateSetListener mDateSetListener = new OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			updateNow();

		}
	};
}
